package com.lyf.spring.demo.controller;

import com.lyf.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/r1")
    public  String r1(String keyword){
        return  "接收参数:" +keyword;
    }

    @RequestMapping("/r2")
    public  String r2(String username,String password){
        return  "接收参数: username:" +username+"password:"+password;
    }

    @RequestMapping("/r5")
    public  String r5(UserInfo userInfo){
        return  "接收参数: userInfo="+userInfo.toString();
    }


    @RequestMapping("/r6")
    //@RequestParam(value = "q")这个q是必存参数，如果置为false就可以了，不然后面的名字没用

    public  String r6(@RequestParam(value = "q",required = false)String keywork){
        return  "接收参数:keywork="+keywork;
    }

    @RequestMapping("/r7")
    //@RequestParam(value = "q")这个q是必存参数，如果置为false就可以了，不然后面的名字没用
    public  String r7(String[] arr){
        return  "接收参数:arr"+arr;
    }
    //在postman里面，写一个arr参数后面跟一个值，写多个证明这数组有多个arr参数，比如
    //arr  1
    //arr  2
    //arr  3   arr[]={1,2,3}
    //第二种是,号分割
    //arr 1，2，3


    //传递集合
    @RequestMapping("/r8")
    public  String r8(@RequestParam List<Integer> list) {
        return  "接收参数:list"+list;
    }

    //传递json
    @RequestMapping("/r9")//@RequestBody表示要接收一个json对象
    public  String r9(@RequestBody UserInfo userInfo) {
        System.out.println(userInfo.getName());
        return  userInfo.toString();
    }

    //从url中获取参数
    @RequestMapping("/artucle/{articleId}")
    //@PathVariable表示取路径变量，符合上面路径的可以
    // {articleId}表示下面articleId的值
    public String r10(@PathVariable Integer articleId){
        return "获取文章ID:"+articleId;
    }

    //路径变量可以表示多个
    @RequestMapping("/artucle1/{type}/{articleId}")
    //@PathVariable("{...}")//可以对上面的路径变量...进行重命名
    public String r11(@PathVariable Integer articleId,@PathVariable("type") String articletype){
        return "获取文章ID:"+articleId+"获取文章类型"+articletype;
    }


    //上传文件
    @RequestMapping("/r12")
    public String r12(@RequestPart("file1") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传

        file.transferTo(new File("D:\\fileShangChuan\\"+file.getOriginalFilename()));
        //文件上传到后面那个地方，
        return "文件获取成功";
    }

    @RequestMapping("/r13")
    public String r13(HttpServletRequest request){
        Cookie[] cookies= request.getCookies();
        if(cookies!=null){
            for (Cookie cookie:cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());
//                  在postman设置cookie，记住postman设置的domain和请求路径要一样

            }
        }
        return "返回cookie成功";
    }

    @RequestMapping("/r14")
    //cookie里面有java的值会被打印
    public String r14(@CookieValue("java") String java){
        return "从Cookie中获取Java的值: "+ java;
    }

    //存储session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //从cookie中获取sessionId,根据sessionId获取Session对象
        //如果sessionId不存在，则创建
        HttpSession session= request.getSession();
        //默认存储在内存中
        //登录的用户名称
        //存储在内存中，终止了运行也就没了
        session.setAttribute("username","zhangsan");
        session.setAttribute("age",17);
        return "设置session成功";
    }
    //获取session的三种写法
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //从cookie中获取sessionId，根据sessionId获取Session对象
        HttpSession session = request.getSession(false);
        //如果用户登录，session有值，未登录，session为null
        if(session==null){
            return  "用户未登录";
        }else{
            //从session中获取登录用户的信息
            String userName=(String) session.getAttribute("username");
            return  "登录用户为:"+userName;
        }
    }

    //获取session方法2
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        //从session中获取登录信息
        String userName=(String)session.getAttribute("username");
        return  "登录用户为"+userName;
    }

    //获取session方法3
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("username") String username){
        return "登录用户为"+username;
    }

    //获取header
    @RequestMapping("getHeader1")
    public String getHeader1(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return  "从header中获取userAgent:"+userAgent;
    }

    @RequestMapping("getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String useragent){
        return "从header中获取userAgent"+useragent;
    }
}
